package com.yc.javaee.tomcat.impl;

import com.google.gson.Gson;
import com.yc.javaee.bean.Result;
import com.yc.javaee.tomcat.HttpServletRequest;
import com.yc.javaee.tomcat.HttpServletResponse;
import com.yc.javaee.tomcat.ServletException;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/getLoginedUser.s")
public class getLoginedUserServlet extends HttpServletImpl{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Object loginedUser = request.getSession().getAttribute("loginedUser");
        Result result;
        if(loginedUser==null){
            result = new Result(0, "尚未登录", null);
        } else {
            result = new Result(1, "已登录", loginedUser);
        }
        response.setStatus(200,"OK");
        response.setContentType("application/json;charset=utf-8");
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}
